package it.nard.controller;

import it.nard.listener.ControlDocumentListener;
import it.nard.persistence.dao.AllevatoriMapper;
import it.nard.persistence.dao.LatteinentrataMapper;
import it.nard.persistence.model.Allevatori;
import it.nard.persistence.model.AllevatoriExample;
import it.nard.persistence.model.Latteinentrata;
import it.nard.sourceframes.AllevatoriPnl;
import it.nard.sourceframes.LatteInEntrataPnl;
import it.nard.sourceframes.MainFrame;
import it.nard.sourceframes.RMozzarellaPnl;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import org.apache.ibatis.session.SqlSession;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
import org.jdesktop.swingx.decorator.HighlighterFactory;



public class AllevatoriController  extends BasicController{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4233357481251876666L;

	public static final String ALLEVATORI_CODE = "allevatori";
	
	
	
	
	public AllevatoriController (){
		super("Allevatori", null);
	}
	

	
	public void select (MouseEvent mevent){
		
		try{
			this.selectedRow = ((AllevatoriPnl)controlPnl).getJTable0().convertRowIndexToModel(((AllevatoriPnl)controlPnl).getJTable0().getSelectedRow());
			
		}catch(Exception e ){	
		}
		if(this.selectedRow==-1)
			return;
		
		
//		System.out.println("ELEMENTO SELEZIONATO : "+selectedRow);
		Allevatori allevatori = (Allevatori)objList.get(selectedRow);
		mainFrame.sendMessage("Selezionato Allevatore :"+allevatori.getNome(), MainFrame.OK_MESSAGE);
		((AllevatoriPnl)controlPnl).getJTextArea0().setText(allevatori.getNote());
		((AllevatoriPnl)controlPnl).getJTextField0().setText(allevatori.getNome());
		((AllevatoriPnl)controlPnl).getJTextField1().setText(allevatori.getCodaziendale());
		((AllevatoriPnl)controlPnl).getJTextField2().setText(allevatori.getNumidentificativo());
		((AllevatoriPnl)controlPnl).getJComboBox0().setSelectedItem(allevatori.getCisterna());
	}
	
	
	
	
	public void insert (){

		Allevatori allevatori = new Allevatori();
		
		if(selectedRow!=-1 || ((AllevatoriPnl)controlPnl).getJTable0().getModel().getRowCount()!=0){
			
			allevatori = (Allevatori)this.objList.get(this.selectedRow);
		}
		
		allevatori.setNome(((AllevatoriPnl)controlPnl).getJTextField0().getText().trim());
		allevatori.setCodaziendale(((AllevatoriPnl)controlPnl).getJTextField1().getText().trim());
		allevatori.setNumidentificativo(((AllevatoriPnl)controlPnl).getJTextField2().getText().trim());
		allevatori.setNote(((AllevatoriPnl)controlPnl).getJTextArea0().getText());
		allevatori.setCisterna(((AllevatoriPnl)controlPnl).getJComboBox0().getSelectedItem().toString()); 

		SqlSession sqlSession = MainController.getSqlSessionFactory().openSession();
		if(selectedRow==-1  || ((AllevatoriPnl)controlPnl).getJTable0().getModel().getRowCount()==0){

					
					
					sqlSession.getMapper(AllevatoriMapper.class).insert(allevatori);
				
					sqlSession.commit();
					mainFrame.sendMessage("Inserito Allevatore :"+allevatori.getNome(), MainFrame.OK_MESSAGE);
					
					objList = sqlSession.getMapper(AllevatoriMapper.class).selectByExampleWithBLOBs(new AllevatoriExample());
					
					DefaultTableModel dtm = (DefaultTableModel)((AllevatoriPnl)controlPnl).getJTable0().getModel();
					
					
					dtm.addRow(new String[]{allevatori.getNome(),allevatori.getCodaziendale(), allevatori.getNumidentificativo(), allevatori.getCisterna()});

					clearPnl();
					
		
		}
		else{
						sqlSession.getMapper(AllevatoriMapper.class).updateByPrimaryKeyWithBLOBs(allevatori);
						
						sqlSession.commit();
						mainFrame.sendMessage("Modificato Allevatore :"+allevatori.getNome(), MainFrame.OK_MESSAGE);
						
						objList = sqlSession.getMapper(AllevatoriMapper.class).selectByExampleWithBLOBs(new AllevatoriExample());
						
						DefaultTableModel dtm = (DefaultTableModel)((AllevatoriPnl)controlPnl).getJTable0().getModel();
						
						
						dtm.setValueAt(allevatori.getNome(), selectedRow, 0);
						dtm.setValueAt(allevatori.getCodaziendale(), selectedRow, 1);
						dtm.setValueAt(allevatori.getNumidentificativo(), selectedRow, 2);
						dtm.setValueAt(allevatori.getCisterna(), selectedRow, 3);
						
					

						clearPnl();
						
					
		}
		sqlSession.close();
		
	}
	
	public void delete (){
	
		SqlSession sqlSession = MainController.getSqlSessionFactory().openSession();
		
		sqlSession.getMapper(AllevatoriMapper.class).deleteByPrimaryKey(((Allevatori)(objList.get(selectedRow))).getId());
	
		sqlSession.commit();
		
		mainFrame.sendMessage("Eliminato Allevatore :"+((Allevatori)(objList.get(selectedRow))).getNome(), MainFrame.OK_MESSAGE);
		DefaultTableModel dtm = (DefaultTableModel) ((AllevatoriPnl)controlPnl).getJTable0().getModel();
		try{
			dtm.removeRow(selectedRow);
		}catch(Exception e ){}
		
		
		clearPnl();
		sqlSession.close();
				
	}
	
	
	
	public List<Allevatori> retriveobjList (){

		SqlSession sqlSession = MainController.getSqlSessionFactory().openSession();
		objList = sqlSession.getMapper(AllevatoriMapper.class).selectByExampleWithBLOBs(new AllevatoriExample());
		
		sqlSession.close();
		return (List<Allevatori>)objList;
	}
	
	
	public AllevatoriPnl getMainPanel (){
		
		if(controlPnl==null){
			 controlPnl = new  AllevatoriPnl(this);
			 
			 JXTable theTable = (JXTable)((AllevatoriPnl)controlPnl).getJTable0();
			 theTable.addHighlighter(HighlighterFactory.createAlternateStriping(Color.getHSBColor(229,229,229), Color.getHSBColor(208, 208, 208))); 
			 AutoCompleteDecorator.decorate(((AllevatoriPnl)controlPnl).getJComboBox0());
			 
			 BasicController.tableDocumentListener.addTableListener(((AllevatoriPnl)controlPnl).getJTextField3(), ((AllevatoriPnl)controlPnl).getJTable0());
			 
			 ((AllevatoriPnl)controlPnl).getJTable0().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			 
			 ControlDocumentListener cdl = BasicController.controlDocumentListener;
			
			 ArrayList<JButton> listButton = new ArrayList<JButton>();
			 listButton.add(((AllevatoriPnl)controlPnl).getJButton0());
			 listButton.add(((AllevatoriPnl)controlPnl).getJButton1());
			
			 
			 ArrayList<JTextField> listMandatory = new ArrayList<JTextField>();
			 listMandatory.add(((AllevatoriPnl)controlPnl).getJTextField0());
			 listMandatory.add(((AllevatoriPnl)controlPnl).getJTextField2());
			 
			 ArrayList<JTextField> listNumeric = new ArrayList<JTextField>();
			 listNumeric.add(((AllevatoriPnl)controlPnl).getJTextField1());
			 
			 cdl.addControlElement(ControlDocumentListener.MANDATORY_FIELD, ((AllevatoriPnl)controlPnl).getJTextField0(), listButton,listMandatory, listNumeric);
			 cdl.addControlElement(ControlDocumentListener.MANDATORY_FIELD, ((AllevatoriPnl)controlPnl).getJTextField2(), listButton,listMandatory, listNumeric);
			 
			 
		
			

		}
		clearPnl();
		DefaultTableModel dtm = (DefaultTableModel)((AllevatoriPnl)controlPnl).getJTable0().getModel();
		dtm.getDataVector().removeAllElements();
		dtm.setNumRows(0);
		dtm.fireTableDataChanged();
		
		
		
		SqlSession sqlSession = MainController.getSqlSessionFactory().openSession();
		objList = sqlSession.getMapper(AllevatoriMapper.class).selectByExampleWithBLOBs(new AllevatoriExample());
		
		dtm = (DefaultTableModel)((AllevatoriPnl)controlPnl).getJTable0().getModel();
		Iterator<Allevatori> iter = (Iterator<Allevatori>)objList.iterator();
		while(iter.hasNext()){
			Allevatori allevatori = iter.next();
			dtm.addRow(new String[]{allevatori.getNome(),allevatori.getCodaziendale(), allevatori.getNumidentificativo(), allevatori.getCisterna()});
		}
		
	
		
		sqlSession.close();
		return (AllevatoriPnl)controlPnl;
	}


	

	@Override
	public String getControllerCode() {
		return ALLEVATORI_CODE;
	}
	
	public void clearPnl(){
		
		((AllevatoriPnl)controlPnl).getJTextArea0().setText("");
		((AllevatoriPnl)controlPnl).getJTextField0().setText("");
		((AllevatoriPnl)controlPnl).getJTextField1().setText("");
		((AllevatoriPnl)controlPnl).getJTextField2().setText("");
		((AllevatoriPnl)controlPnl).getJComboBox0().setSelectedIndex(0);
		
		this.selectedRow = -1;
		
	}




	@Override
	public void onDateSelect() {
		// TODO Auto-generated method stub
		
	}


	





	






	
	
	
}

